package com.xl.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.xl.anno.MyMapperScan;
import com.xl.beans.OtherBean;
import com.xl.beans.OtherBean2;
import com.xl.imports.MyImportBeanDefinitionRegister;
import com.xl.imports.MyImportSelector;
import com.xl.service.UserService;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;

@Configuration //标注当前类是一个配置类（配置文件）并且+@Component
//<context:component-scan base-package="com.xl"/>
@ComponentScan(basePackages = {"com.xl"}) //只要一个包的时候可以直接"com.xl"
//<context:property-placeholder location="jdbc.properties" />
@PropertySource("classpath:jdbc.properties")
//@Import(OtherBean.class)
//@Import(MyImportSelector.class)
//@Import(MyImportBeanDefinitionRegister.class)
@MyMapperScan
//Mapper的接口扫描
@MapperScan("com.xl.mapper")
public class SpringConfig {


    @Bean
    public DataSource dataSource(@Value("${jdbc.driver}") String driverClassName,
                                 @Value("${jdbc.url}") String url,
                                 @Value("${jdbc.username}") String username,
                                 @Value("${jdbc.password}") String password) {
        DruidDataSource dataSource = new DruidDataSource();
        System.out.println("driverClassName:"+driverClassName);
        System.out.println("url:"+url);
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        //设置4个基本参数
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }
}
